﻿using System;
using System.Reflection;

using HttpApiGateway.Collect;
using HttpApiGateway.Config;
using HttpApiGateway.Interface;
using HttpService.Config;
using RpcHelper;

namespace HttpApiGateway
{
    /// <summary>
    /// 模块的基类
    /// </summary>
    public class BasicApiModular : IApiModular
    {
        private string _AssemblyName;
        public BasicApiModular(string name)
        {
            this.ServiceName = name;
        }
        /// <summary>
        /// 模块说明
        /// </summary>
        public IModularConfig Config
        {
            get;
        } = new ModularConfig();

        public string ServiceName
        {
            get;
        }

        public string ApiRouteFormat => this.Config.ApiRouteFormat;

        public void InitModular()
        {
            this._LoadModular();
            this.Init();
        }
        protected void AddFileDir(FileDirConfig dir)
        {
            HttpService.HttpService.AddFileDir(dir);
        }
        private void _LoadModular()
        {
            Type type = this.GetType();
            Assembly assembly = type.Assembly;
            this._AssemblyName = assembly.FullName;
            RpcClient.RpcClient.Load(assembly);
            Type[] types = assembly.GetTypes();
            using (IResourceCollect resource = ResourceCollect.Create(this))
            {
                types.ForEach(a =>
                {
                    if (a.IsInterface)
                    {
                        return;
                    }
                    else if (UnityCollect.RegisterApi(a))
                    {
                        IApiController api = UnityCollect.GetApi(a);
                        ApiRouteCollect.RegApi(a, api, this, resource.RegRoute);
                    }
                    else if (UnityCollect.RegisterGateway(a))
                    {
                        ApiRouteCollect.RegApi(a, this, resource.RegRoute);
                    }
                    else if (UnityCollect.RegisterUpBlockFileApi(a))
                    {
                        ApiRouteCollect.RegBlockUpApi(a, this, resource.RegRoute);
                    }
                });
            }
        }

        protected virtual void Init()
        {

        }

        public void Start()
        {
        }

        public void Dispose()
        {
        }

        public bool CheckIsContain(Type type)
        {
            return this._AssemblyName == type.Assembly.FullName;
        }
    }
}
